#include "draw_2d.h"

#include "graphics.h"

////////////////////////////////////////////////////////////////////////////////

void Draw2D_drawLine(const Vector2& p1, const Vector2& p2, Colour colour)
{
  Material material;
  VertexP2C vertex[2];
  
  vertex[0].pos = p1;
  vertex[0].colour = colour;
  vertex[1].pos = p2;
  vertex[1].colour = colour;
  
  Graphics_initMaterial(&material);
  
  Graphics_draw(VertexP2C::type, PRIMITIVE_LINE, material, 2, vertex, 0, NULL);
}

void Draw2D_drawCircle(const Vector2& pos, float radius, int num_segs,
  Colour colour)
{
  const float da = 360.0f / num_segs;
  Material material;
  VertexP2C vertex[num_segs];
  
  for (int i = 0; i < num_segs; i++)
  {
    vertex[i].pos.x = pos.x - radius * Math_sind(da * i);
    vertex[i].pos.y = pos.y - radius * Math_cosd(da * i);
    vertex[i].colour = colour;
  }
  
  Graphics_initMaterial(&material);
  
  Graphics_draw(VertexP2C::type, PRIMITIVE_LINE_LOOP, material, num_segs, vertex,
    0, NULL);
}

void Draw2D_fillCircle(const Vector2& pos, float radius, int num_segs,
  Colour colour)
{
  const float da = 360.0f / num_segs;
  Material material;
  VertexP2C vertex[num_segs];
  
  for (int i = 0; i < num_segs; i++)
  {
    vertex[i].pos.x = pos.x - radius * Math_sind(da * i);
    vertex[i].pos.y = pos.y - radius * Math_cosd(da * i);
    vertex[i].colour = colour;
  }
  
  Graphics_initMaterial(&material);
  
  Graphics_draw(VertexP2C::type, PRIMITIVE_TRI_FAN, material, num_segs, vertex,
    0, NULL);
}

void Draw2D_drawArc(const Vector2& pos, float radius, float a1, float a2,
  int num_segs, Colour colour)
{
  const float da = Math_angleWrap360(a2 - a1) / num_segs;
  Material material;
  VertexP2C vertex[num_segs + 2];
  
  vertex[0].pos = pos;
  vertex[0].colour = colour;
  
  for (int i = 0; i <= num_segs; i++)
  {
    vertex[i + 1].pos.x = pos.x + radius * Math_cosd(Math_angleWrap360(a1 + da * i));
    vertex[i + 1].pos.y = pos.y + radius * Math_sind(Math_angleWrap360(a1 + da * i));
    vertex[i + 1].colour = colour;
  }
  
  Graphics_initMaterial(&material);
  
  Graphics_draw(VertexP2C::type, PRIMITIVE_LINE_LOOP, material,
    num_segs + 2, vertex, 0, NULL);
}
